package com.neu.daoimpl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import com.neu.dao.UsersDao;
import com.neu.entity.TBUsers;
import com.neu.tool.JDBCTool;


public class UsersDaoImpl implements UsersDao {

	@Override
	public boolean loginValidate1(TBUsers tu) throws SQLException {
		Connection conn=JDBCTool.getConnection();
		boolean flag = false;
		String sql = "select * from tb_users where name=? and pass=?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, tu.getName());
		ps.setString(2, tu.getPass());
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
			flag=true;
		}
		return false;
	}

	/*
	 * 方法说明:
	 * 		根据传入的参数(用户名和密码)来完成登录的验证,成功返回true,否则返回false
	 * 方法步骤:
	 * 		
	 */
	@Override
	public boolean loginValidate(String name, String pass) throws SQLException {
		Connection conn = JDBCTool.getConnection();
		boolean flag = false;
		String sql = "select * from tb_users where name=? and pass=?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, name);
		ps.setString(2, pass);
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
			flag=true;
		}
		return flag;
	}
/**
 *   注册
 */
@Override
public boolean registerUser(TBUsers tu) throws SQLException {
	Connection conn =JDBCTool.getConnection();
	boolean flag = false;
	String sql = "insert into tb_users value(null,?,?)";
	PreparedStatement ps = conn.prepareStatement(sql);
	ps.setString(1, tu.getName());
	ps.setString(2, tu.getPass());
	int  a = ps.executeUpdate();
	if(a>0) {
		flag=true;
	}
	return flag;
}
/**
 *    测试一下 
 * @param args
 */
//public static void main(String[] args) {
//	UsersDao ud = new UsersDaoImpl();
//	TBUsers tb = new TBUsers("没进去", "123");
//	try {
//		boolean b=ud.registerUser(tb);
//		System.out.println("成功:"+b);
//	} catch (SQLException e) {
//		// TODO Auto-generated catch block
//		e.printStackTrace();
//	}
//}
/**
 *   更新操作
 */
@Override
public boolean update(TBUsers tu) throws SQLException {
	Connection conn = JDBCTool.getConnection();
	boolean flag = false;
	String sql = "update tb_users set name=?,pass=? where id=?";
	PreparedStatement ps = conn.prepareStatement(sql);
	ps.setString(1, tu.getName());
	ps.setString(2, tu.getPass());
	ps.setInt(3,tu.getId());
	int a = ps.executeUpdate();
	if(a>0) {
		flag = true;
	}
	return flag;
}
		/**
		 *   删除
		 */
@Override
public boolean delete(TBUsers tu) throws SQLException {
	Connection conn = JDBCTool.getConnection();
	boolean flag = false;
	String sql = "delete from tb_users where id=?";
	PreparedStatement ps = conn.prepareStatement(sql);
	ps.setInt(1, tu.getId());
	int a = ps.executeUpdate();
	if(a>0) {
		flag=true;
	}
	return flag;
}
}
